import { Component } from '@angular/core';
import { IonicPage, NavController, NavParams,App } from 'ionic-angular';
import {ApiProvider} from "../../providers/api/api";

/**
 * Generated class for the CreateBullentPage page.
 *
 * See https://ionicframework.com/docs/components/#navigation for more info on
 * Ionic pages and navigation.
 */

@IonicPage()
@Component({
  selector: 'page-create-bullent',
  templateUrl: 'create-bullent.html',
})
export class CreateBullentPage {

  bullentTxt='';//弹幕内容

  unit_id;
  classCourseId;
  studentId;
  constructor(public navCtrl: NavController, public navParams: NavParams,private http:ApiProvider,public appCtrl:App) {

  }

  ionViewDidLoad() {
  }

  ionViewDidEnter()
  {
    this.unit_id=this.navParams.get("unitId");
    this.classCourseId=this.navParams.get("classCourseId");
    this.studentId=this.navParams.get("studentId");
  }

  createBullent()
  {
    let reg:RegExp=/^[a-zA-Z0-9,，。.！？；“” '' 《》\u4e00-\u9fa5]+$/;//弹幕只可以输入汉字大小写英文数字及逗号 句号 感叹号 问号 分号 引号 书名号
    let regValidZH=reg.test(this.bullentTxt.trim());
    let regValidEmoji=this.isEmojiCharacter(this.bullentTxt.trim());

    if(this.bullentTxt.trim()=="")
    {
      this.http.showToast("弹幕内容不能为空！","bottom");
    }
    else if(!regValidZH||!regValidEmoji)
    {
      this.http.showToast("弹幕含有非法字符！","bottom");
    }
    else
    {
      this.http.post("/bullentController/addBullentContent.do",{bullent_content:this.bullentTxt.trim(),unit_id:this.unit_id,classcourse_id:this.classCourseId,
        user_id:this.studentId}).subscribe(item=>{
          if(item["code"]!="0")
          {
            this.http.showToast(item["msg"],'bottom')
          }
          else
          {
            this.bullentTxt='';
            this.http.showToast("弹幕发送成功！",'bottom');
          }
      });
    }
  }

  //验证emoji表情
  isEmojiCharacter(substring) {
    let flag=true;
    for (let i = 0; i < substring.length; i++) {
      let hs = substring.charCodeAt(i);
      if (0xd800 <= hs && hs <= 0xdbff) {
        if (substring.length > 1) {
          let ls = substring.charCodeAt(i + 1);
          let uc = ((hs - 0xd800) * 0x400) + (ls - 0xdc00) + 0x10000;
          if (0x1d000 <= uc && uc <= 0x1f77f) {
            return false;
          }
        }
      } else if (substring.length > 1) {
        let ls = substring.charCodeAt(i + 1);
        if (ls == 0x20e3) {
          return false;
        }
      } else {
        if (0x2100 <= hs && hs <= 0x27ff) {
          return false;
        } else if (0x2B05 <= hs && hs <= 0x2b07) {
          return false;
        } else if (0x2934 <= hs && hs <= 0x2935) {
          return false;
        } else if (0x3297 <= hs && hs <= 0x3299) {
          return false;
        } else if (hs == 0xa9 || hs == 0xae || hs == 0x303d || hs == 0x3030 || hs == 0x2b55 || hs == 0x2b1c || hs == 0x2b1b || hs == 0x2b50) {
          return false;
        }
      }
    }
    return flag;
  }
}
